﻿<script>
    $(function () {
        var _vm = new ViewModel();
        _vm.Init();

    });

    function ViewModel() {
        var me = this;
        //注意顺序
        me.DataTable = new DataTable(me, "#table");
        var mixinsobj = {
            data: {
                Menus : @this.Raw(this.ViewBag.Menus),
            },
        };
        me.Dialog = new Dialog(me, "#frmEdit", mixinsobj);
        me.app = new App(me, "#app");
        me.app.DeleteUrl = "@this.Href("~/SystemManage/Module/Delete")";
        Extend.call(me);
    }


    function Extend() {
        var me = this;
        me.Init = function () {
            me.LoadModels();
        }
        me.LoadModels = function (page) {
            me.DataTable.SelectedModel = null;
            var data = me.app.SearchModel;
            data.page = page || 1;
            $hits.get("@this.Href("~/SystemManage/Module/GetModels")", data, function (result) {
                me.DataTable.SetModels(result.Data);
            }
         );
        }
        me.OnOpen = function (id) {

            me.Dialog.Model = me.Dialog.Model.Data;//取下一层级数据
        }
        me.OnSave = function () {
            var model = me.Dialog.Model;

            if (!$("#form1").formValid()) {
                return false;
            }

            if (me.Dialog.EditModel) {
                $hits.post("@this.Href("~/SystemManage/Module/Update")", model, function (result) {
                    me.Dialog.Close();
                    me.LoadModels();
                    $hits.msg(result.Msg);
                }
                );
            }
            else {
                $hits.post("@this.Href("~/SystemManage/Module/Add")", model, function (result) {
                    me.Dialog.Close();
                    me.LoadModels();
                    $hits.msg(result.Msg);
                }
                );
            }
        }
    }
    
</script>
